package erfgame.core.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.awt.Point;
import java.util.Collection;
import java.util.List;

import org.junit.Test;

import erfgame.core.world.terrain.GridRectangle;


@SuppressWarnings(value="unchecked")
public class GridRectangleTester {

	private boolean assertEquivalent( Object[] c1, Collection c2 ) {
		if( c1.length == c2.size() ) {
			for( Object o : c1 ) { 
				if( !c2.contains(o) ) {
					return false;
				}
			}
			return true;
		} else {
			return false;
		}
	}
	
	@Test
	public void testRectangleWestEast() {
		GridRectangle r = new GridRectangle(1, 1, 2, 2 );
		assertEquals(r.getWestX(), 1);
		assertEquals(r.getWestY(), 1);
		assertEquals(r.getEastX(), 2);
		assertEquals(r.getEastY(), 2);
		assertEquals(r.getSouthX(), 2);
		assertEquals(r.getSouthY(), 3);
		assertEquals(r.getNorthX(), 1);
		assertEquals(r.getNorthY(), 0);
		
		List<Point> area = r.getArea();
		
		assertTrue( 
				this.assertEquivalent(
						new Object[]{
								new Point(1,1), 
								new Point(2,2), 
								new Point(1,0),
								new Point(2,1),
								new Point(1,2),
								new Point(2,3)
						}, area
				)
		);
	}

	@Test
	public void testRectangleEastWest() {
		GridRectangle r = new GridRectangle(3, 2, 2, 1 );
		assertEquals(r.getWestX(), 2);
		assertEquals(r.getWestY(), 1);
		assertEquals(r.getEastX(), 3);
		assertEquals(r.getEastY(), 2);
		assertEquals(r.getSouthX(), 3);
		assertEquals(r.getSouthY(), 3);
		assertEquals(r.getNorthX(), 2);
		assertEquals(r.getNorthY(), 0);
	}

	@Test
	public void testRectangleNorthSouth() {
		GridRectangle r = new GridRectangle(1, 0, 1, 6 );
		assertEquals(r.getWestX(), 0);
		assertEquals(r.getWestY(), 3);
		assertEquals(r.getEastX(), 3);
		assertEquals(r.getEastY(), 3);
		assertEquals(r.getSouthX(), 1);
		assertEquals(r.getSouthY(), 6);
		assertEquals(r.getNorthX(), 1);
		assertEquals(r.getNorthY(), 0);
	}

	@Test
	public void testRectangleSouthNorth() {
		GridRectangle r = new GridRectangle(3, 7, 3, 5 );
		assertEquals(r.getWestX(), 2);
		assertEquals(r.getWestY(), 6);
		assertEquals(r.getEastX(), 3);
		assertEquals(r.getEastY(), 6);
		assertEquals(r.getSouthX(), 3);
		assertEquals(r.getSouthY(), 7);
		assertEquals(r.getNorthX(), 3);
		assertEquals(r.getNorthY(), 5);
	}
	
	@Test
	public void testRectangleZeroNE() {
		GridRectangle r = new GridRectangle(1, 3, 2, 6 );
		assertEquals(r.getWestX(), 1);
		assertEquals(r.getWestY(), 3);
		assertEquals(r.getEastX(), 2);
		assertEquals(r.getEastY(), 6);
		assertEquals(r.getSouthX(), 2);
		assertEquals(r.getSouthY(), 6);
		assertEquals(r.getNorthX(), 1);
		assertEquals(r.getNorthY(), 3);
	}


	@Test
	public void testRectangleZeroSE() {
		GridRectangle r = new GridRectangle(1, 6, 2, 4 );
		assertEquals(r.getWestX(), 1);
		assertEquals(r.getWestY(), 6);
		assertEquals(r.getEastX(), 2);
		assertEquals(r.getEastY(), 4);
		assertEquals(r.getSouthX(), 1);
		assertEquals(r.getSouthY(), 6);
		assertEquals(r.getNorthX(), 2);
		assertEquals(r.getNorthY(), 4);
	}
	
	@Test
	public void testRectangleZero() {
		GridRectangle r = new GridRectangle(1, 3, 1, 3 );
		assertEquals(r.getWestX(), 1);
		assertEquals(r.getWestY(), 3);
		assertEquals(r.getEastX(), 1);
		assertEquals(r.getEastY(), 3);
		assertEquals(r.getSouthX(), 1);
		assertEquals(r.getSouthY(), 3);
		assertEquals(r.getNorthX(), 1);
		assertEquals(r.getNorthY(), 3);
	}

}
